//package com.ruoyi.common.utils.fabric;
//
//import com.ruoyi.common.properties.FabricProperties;
//import lombok.extern.slf4j.Slf4j;
//import org.hyperledger.fabric.gateway.Contract;
//import org.hyperledger.fabric.gateway.Gateway;
//import org.hyperledger.fabric.gateway.Identities;
//import org.hyperledger.fabric.gateway.Network;
//import org.hyperledger.fabric.gateway.Wallet;
//import org.hyperledger.fabric.gateway.Wallets;
//import org.hyperledger.fabric.sdk.Channel;
//import org.hyperledger.fabric.sdk.HFClient;
//import org.hyperledger.fabric.sdk.Orderer;
//import org.hyperledger.fabric.sdk.Peer;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.io.Reader;
//import java.net.URI;
//import java.nio.charset.StandardCharsets;
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.security.InvalidKeyException;
//import java.security.PrivateKey;
//import java.security.cert.CertificateException;
//import java.security.cert.X509Certificate;
//
///**
// * Fabric 工具类
// *
// * @author Henriport
// * @since 2021/11/16
// */
//@Slf4j
//@Component
//public class FabricUtils {
//    @Resource
//    private FabricProperties fabricProperties;
//
//    private Network network;
//    private Contract contract;
//
//    public Network getNetwork() {
//        return this.network;
//    }
//
//    public Contract getContract() {
//        return this.contract;
//    }
//
////    @PostConstruct
//    private void init() {
//        try {
//            String resourcePath = this.getClass().getResource("/").toURI().toString();
//            System.out.println(resourcePath);
//            X509Certificate certificate = readX509Certificate(Paths.get(new URI(resourcePath+fabricProperties.getCertificatePath())));
//            PrivateKey privateKey = getPrivateKey(Paths.get(new URI(resourcePath+fabricProperties.getPrivateKeyPath())));
//            Wallet wallet = Wallets.newInMemoryWallet();
//            wallet.put(fabricProperties.getUser(), Identities.newX509Identity(fabricProperties.getMspId(), certificate, privateKey));
//            //根据配置文件 获取Fabric网络连接对象
//            Gateway.Builder builder = Gateway.createBuilder()
//                .identity(wallet, "admin")
//                .networkConfig(Paths.get(new URI(resourcePath+fabricProperties.getNetworkConfigPath())));
//            //连接网关
//            Gateway gateway = builder.connect();
//            //获取通道
//            network = gateway.getNetwork(fabricProperties.getChannelName());
//            //获取合约对象
//            contract = network.getContract(fabricProperties.getContractName());
//        } catch (Exception e) {
//            log.error("FabricUtils 初始化失败: {}", e.getMessage(), e);
//        }
//    }
//
//    private static X509Certificate readX509Certificate(final Path certificatePath) throws IOException, CertificateException {
//        try (Reader certificateReader = Files.newBufferedReader(certificatePath, StandardCharsets.UTF_8)) {
//            return Identities.readX509Certificate(certificateReader);
//        }
//    }
//
//    private static PrivateKey getPrivateKey(final Path privateKeyPath) throws IOException, InvalidKeyException {
//        try (Reader privateKeyReader = Files.newBufferedReader(privateKeyPath, StandardCharsets.UTF_8)) {
//            return Identities.readPrivateKey(privateKeyReader);
//        }
//    }
//}
